package com.aaa.service.system.impl;

import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.dao.system.RoleDao;
import com.aaa.entity.system.Menu;
import com.aaa.entity.system.Role;
import com.aaa.service.system.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-09-06 19:42:17
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleDao.queryById(roleId);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @InsertAnn
    @Override
    public Role insert(Role role) {
        this.roleDao.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @UpdateAnn
    @Override
    public Role update(Role role) {
        int update = roleDao.update(role);
        if (update>0){
            return role;
        }else {
            return null;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.roleDao.deleteById(roleId) > 0;
    }

    @Override
    public List<Role> queryAllRole() {
        return roleDao.queryAllRole();
    }

    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    @Override
    public List<Integer> findSelectMenu(int roleId) {
        return roleDao.findSelectMenu(roleId);
    }


    @Override
    public void insertRoleMenu(int roleId, String[] menusId) {
        for (int i = 0; i < menusId.length; i++) {
            roleDao.insertRoleMenu(roleId, menusId[i]);
        }
    }

    @Override
    public void updateRoleMenu(Integer roleId, String[] menusId) {
        roleDao.deleteRoleMenu(roleId);
        for (int i = 0; i < menusId.length; i++) {
            roleDao.insertRoleMenu(roleId, menusId[i]);
        }

    }


}
